package com.hcy.app.screen.cipher

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Checkbox
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.hcy.app.LocalNavController
import com.hcy.app.R
import kotlin.math.roundToInt

internal const val TAG = "cipher"

//@Preview
//@Composable
//fun Home_Page_Pre(){
////    Home_Page()
//}


@Composable
fun Home_Page(viewModel: CipherViewModel=hiltViewModel()){
    val navController = LocalNavController.current
    val cipherState by viewModel.cipherState.collectAsState()
    var operation by remember { mutableStateOf(false) }
    var selected = remember { mutableStateListOf<Long>() }
    Box {
        Column(
            modifier = Modifier.systemBarsPadding().fillMaxSize().background(Color(0xFFE8E096))
        ) {
            // 工具栏
            Row(
                modifier = Modifier.fillMaxWidth().background(Color(0xFF8AECC9)),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(
                    onClick = {
                        operation=!operation
                    },
                    content = { Text(text = if ( operation ) "取消" else "操作") },
                    )

                Row {
                    if ( operation ){
                        TextButton(
                            onClick = {
                                viewModel.deleteNote(selected)
                                operation = !operation
                                selected.clear()
                            },
                            content = { Text(text = "删除") }
                        )
                    }
                }
            }

            // 记录列表
            Column(
                modifier= Modifier.padding(top = 10.dp).weight(1f)
                    .verticalScroll(rememberScrollState())
            ){
                cipherState.noteList.forEach {
                    Note_item(
                        it,
                        operation,
                        modifier = Modifier.clickable{
                        navController.navigate("cipher/edit/${it.id}")
                    }, checkCall = { checked->
                        if (checked)selected.add(it.id) else selected.remove(it.id)
                        }
                    )
                }
            }
            // 底部
            Row(
                modifier = Modifier.fillMaxWidth().background(Color(0xFF75E6F5)),
                horizontalArrangement = Arrangement.SpaceAround
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.clickable{
                        navController.navigate("cipher/secret")
                    }
                ){
                    Icon(
                        imageVector = ImageVector.vectorResource(id = R.drawable.lock),
                        modifier = Modifier.width(30.dp),
                        contentDescription = "密钥",
                        tint = Color.White
                    )
                    Text(
                        text = "密钥",
                        fontSize = 12.sp,
                        fontWeight = FontWeight.Light
                    )
                }
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.clickable{
                        navController.navigate("cipher/contact")
                    }
                ){
                    Icon(
                        imageVector = ImageVector.vectorResource(id = R.drawable.contacts),
                        modifier = Modifier.width(30.dp),
                        contentDescription = "联系人",
                        tint = Color.White
                    )
                    Text(
                        text = "联系人",
                        fontSize = 12.sp,
                        fontWeight = FontWeight.Light
                    )
                }
            }
        }
        var offset by remember { mutableStateOf(Offset(0f,-60f)) }
        // 浮动在右下角的元素,
        // FloatingActionButton 相对于 Button 会强化突出显示的效果
        FloatingActionButton(
            onClick = {
                navController.navigate("cipher/edit/0")
            },
            modifier = Modifier
                .align(Alignment.BottomEnd) // 定位到右下角
                .padding(16.dp)
                .offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) }
                .pointerInput(Unit) {
                    detectDragGestures { _, dragAmount ->
                        offset += dragAmount
                    }
                },
            containerColor = MaterialTheme.colorScheme.primary
        ) {
            Icon(Icons.Default.Add, "添加")
        }
    }
}

@Composable
private fun Note_item(item: NoteItem, isShow: Boolean, modifier: Modifier= Modifier,checkCall: (Boolean)->Unit){
    var checked by remember { mutableStateOf(false) }

    val lables = item.label.split(",")
    Surface(
        modifier = modifier.fillMaxWidth().padding(bottom = 10.dp),
        shape = RoundedCornerShape(10.dp)
    ) {
        Row {
            Column(
                modifier = modifier.weight(1f).padding(10.dp)
            ){
                Row {
                    Text(
                        text = item.title,
                        maxLines = 1
                    )
                }
                Row {
                    lables.forEach { it->
                        Text(it)
                    }
                }
            }
            if (isShow){
                Checkbox(
                    checked = checked,
                    onCheckedChange = {
                        checked=it
                        checkCall(checked)
                    }
                )
            }
        }
    }
}


